package com.wx.activitys.base.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.wx.activitys.base.model.SSMResponse;
import com.wx.activitys.base.utils.BaseUtils;
import com.wx.activitys.drp.JsonResult;

/**
 * DESCRIPTION: AUTHOR: Qinlong DATE: 2017/8/25
 */

@Controller
@RequestMapping(value = "/interface")
public class SMSInterfaceController {
	public static final String charset = "utf-8";
	// 用户平台API账号(非登录账号,示例:N1234567)
	public static String account = "N5676872";
	// 用户平台API密码(非登录密码)
	public static String pswd = "erduiCESHI17";

	/**
	 * 发送短信接口
	 */
	@RequestMapping(value = { "/sms.html" }, method = RequestMethod.GET)
	@ResponseBody
	public JsonResult getimg(HttpServletRequest request, HttpSession session) {
		GetUserInfoResponse userInfo = ((GetUserInfoResponse) request.getSession().getAttribute("userInfo"));
		if (userInfo != null) {
			// 短信接口1
			String mobile = request.getParameter("mobile");
			PostMethod postMethod = new PostMethod("http://sms.dzd.com/v4/sms/send.do");
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
			int VCode = BaseUtils.getRandNum();
			session.getAttribute("VCode");
			String timeStamp = format.format(new Date());
			String uid = "4119";
			String key = "e1b624f0cc91ca28bc157a8f304ba78a";
			postMethod.addParameter("uid", uid);
			postMethod.addParameter("timestamp", timeStamp);
			try {
				postMethod.addParameter("sign", BaseUtils.getSign(uid, key, timeStamp));// sign-为安全验证的密匙,需要计算
			} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
			postMethod.addParameter("mobile", mobile);
			postMethod.addParameter("text", "【嘉华金融】您的验证码" + VCode);
			HttpClient httpClient = new HttpClient();
			httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
			try {
				httpClient.executeMethod(postMethod);
				System.out.println(postMethod.getResponseBodyAsString());
				String response = postMethod.getResponseBodyAsString();
				JSONObject jsStr = JSONObject.parseObject(response);
				SSMResponse ssmResponse = JSONObject.toJavaObject(jsStr, SSMResponse.class);
				if (ssmResponse.getCode().equals("0")) {
					return JsonResult.success("1", VCode + "");
				} else {
					return JsonResult.success("2", ssmResponse.getMsg());
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return JsonResult.exception("系统错误");
	}
	/*
	 * //短信接口2 String mobile = request.getParameter("mobile"); int VCode =
	 * BaseUtils.getRandNum(); //请求地址请登录253云通讯自助通平台查看或者询问您的商务负责人获取 String
	 * smsSingleRequestServerUrl = "http://smssh1.253.com/msg/send/json"; //
	 * 短信内容 String msg =
	 * "【嘉华金融】您的注册手机验证码"+VCode+"，有效期5分钟。您正在注册使用嘉华金融平台，若非本人操作，请及时联系客服。"; //手机号码
	 * String phone = mobile; //状态报告 String report= "true";
	 * 
	 * SmsSendRequest smsSingleRequest = new SmsSendRequest(account, pswd, msg,
	 * phone,report);
	 * 
	 * String requestJson = JSON.toJSONString(smsSingleRequest);
	 * 
	 * String response =
	 * ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
	 * 
	 * SmsSendResponse smsSingleResponse = JSON.parseObject(response,
	 * SmsSendResponse.class);
	 * 
	 * if(smsSingleResponse.getCode().equals("0")){ return
	 * JsonResult.success("1",VCode+""); }else{ return
	 * JsonResult.success("2",smsSingleResponse.getErrorMsg()); } }
	 */

}
